/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2016-1-21
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","sysmanMain", "bizProto","btnServ","msgServ","flowInit"],
		function($,datagridServ, sysmanMain, bizProto,btnServ,msgServ,flowInit) {

		'use strict';

		var _typeUrl = '/dictionary/getTree.action',_rootpath,
			item_code_dic_id = 24,
			rootId = 'panel-supply-donation',
			edit_window_id='supply-donation-win',
			el_width = 300,el_big_width=674,window_width=900,window_height=600
		
		var public_method ={
			
			/**
			 * 初始化新增页面
			 * @param _options.record 列表选中行
			 * @param _options.form 页面form对象
			 */
			/*initElement:function(_options){
				
			},*/
			
			initElement:function(_options){
				if(window.console){
					console.info(' recipel init loaded and startting ...')
				}
				var queryUrl = '';
				
				var _el =  $('#' + edit_window_id + ' #dg-inner-donation')
				if(_el.length==1){
				 _el.datagrid({
						url : queryUrl,
						height : 100,						
						onClickCell:private_method.editCell,
						columns:[[
							{field:'ck',checkbox:"true"},        
							{field:'device_name',title:'设备名称',halign:'center',width:30,editor:'text'},
							{field:'device_standard',title:'规格',halign:'center',width:30,editor:'text'},
							{field:'device_number',title:'数量',halign:'center',width:30,editor:'text'},
							{field:'device_price',title:'单价',halign:'center',width:30,editor:'text'}
						]],					
					    rownumbers:true,
						animate: true,										
						striped:true,						
						border:true,					
						idField: 'id',										
						fitColumns: true,
						toolbar:'#'+ edit_window_id + ' #inner-table-donation'
					})
				}	
			},
			
			/**
			 * @param _options.record
			 */
			/*buttonExtends:function(){
				return {'查看详情':private_method.detailInfo}
			},*/
			
			buttonExtends:function(){
				var donation_inner_table = $('#' + edit_window_id + ' #dg-inner-donation')
				return {
					'耗材清单':private_method.detailInfo,
					'添加设备':function(){
						$(donation_inner_table).datagrid('appendRow',{})
					},
					'移除设备':function(){
						var record = $(donation_inner_table).datagrid('getSelected');
						if(record){
							var rowindex =$(donation_inner_table).datagrid('getRowIndex',record);
							
							$(donation_inner_table).datagrid('deleteRow',rowindex);
						}else{
							msgServ.Msgslide('请选择删除的记录');
						}
					}
				}
			},
			
			closeWindow:function(){
				$('#'+edit_window_id).empty()
				var _moduleConfig = sysmanMain.getCurrentModuConfig();
				_moduleConfig.initParam = {editable:true};
				sysmanMain.setCurrentModuConfig(_moduleConfig);
			},
			/**
			 * @param _options.rootId
			 * @param _options.conditionSelector
			 * @param _options.
			 */
			initList:function(_options){
				private_method.initCondition(_options)
			},
			reCreateField:function(_options){
				private_method.reCreateField(_options)
			},
			rowDblClick:function(index,row){
				private_method.rowDblClick(index,row)
			}
			
		}
		var private_method = {
			//查看详情
			detailInfo:function(){
				if(window.console){
					console.info('show detail loaded...')
				}
				
				var deptEl = $('#'+edit_window_id + ' #litigant_deptname')
				
				if(deptEl.length>0){
					var deptValue = $(deptEl).textbox('getValue');
					var _param = {sqks:deptValue,over_days_ge:50,sxzt:'在办'}
					var initQueryParams = {condition:_param}
					
					
					sysmanMain.setModuConf('dutiesInfo',{initQueryParams:initQueryParams})
					
					sysmanMain.dutiesInfoSelect()
				}
				
			},
			rowDblClick:function(index,row){
				
				var editWindow = edit_window_id;
				var _param = {}
				var _moduleConfig = sysmanMain.getCurrentModuConfig();
				if(!_moduleConfig){
					_moduleConfig={}
				}
				
				var flow_state_el = $('#'+rootId+' form.condition-form #flow_state')
				var flow_state_value = $(flow_state_el).combobox('getValue')
				if(flow_state_value && flow_state_value==='finished'){
					_param.editable = false
				}else{
					_param.editable = true
				}
				_moduleConfig.initParam = _param
				sysmanMain.setCurrentModuConfig(_moduleConfig)
				
				var $grid= $('#'+rootId +" table.dg-list");
				var $edit_window = $('#' + editWindow);
				var _url =  '/'+sysmanMain.getRootPath() +'/supplyDonation/query/editPage.action';
				var _dgparam = {grid:$grid,window:$edit_window,href:_url,width:window_width,height:window_height,flag:true,existCheckElement:'form'}
								
				_dgparam.onClose=public_method.closeWindow
				
				datagridServ.windowEdit(_dgparam);
			},
			/**
			 * 初始化条件查询框
			 * @param _options.rootId 窗口根节点id
			 * @param _options.conditionSelector 条件框节点选择器
			 */
			initCondition:function(_options){
				if(window.console){
					console.info('recipel init loaded and starting...')
				}
				
				flowInit.initList(_options)
			},
			reCreateField:function(_options){
				if(window.console){
					console.info(' supply reCreateField loaded and startting ...')
				}
				var data = _options.data
				var $form = _options.form
				_rootpath = '/'+sysmanMain.getRootPath()
				
				var normal_param = {required:true,editable:true}
				var normal_width_param = $.extend({},normal_param,{width:el_width})
				var big_width_param = $.extend({},normal_param,{width:el_big_width,multiline:true,height:50})
				var deptQueryUrl='/department/query/getTreeList.action'
				var selectedRecord = $('#' +rootId +' table.dg-list').datagrid('getSelected')
				var fields = data.editFilter.enabled.split(',')
				if(fields && (fields.length>0)){
					
					for(var i =0;i<fields.length;i++){
						var _el =$($form).find('#' + fields[i])
						switch(fields[i]){
							case 'bizno':
							$(_el).textbox(normal_width_param)
							break;
							case 'inquiry_date':
							$(_el).datebox($.extend({readonly:false},normal_width_param))
							break;
							case 'inquiry_reason':
							$(_el).textbox(big_width_param)
							break;
							case 'litigant_content':
							$(_el).textbox(big_width_param)
							break;
							case 'decide_content':
							$(_el).textbox(big_width_param)
							break;
							
							case 'affirm_content':
							$(_el).textbox(big_width_param)
							break;
							case 'litigant_name':
							$(_el).textbox({
								required:true,
								readonly:false,
								onClickButton:function(data){
									var $dept_window = $('#win-dept-user');
									
									$($dept_window).attr('callback-id','supply-donation-win #litigant_id')							
									$($dept_window).attr('callback-value','supply-donation-win #litigant_name')
									
									$($dept_window).attr('callback-deptid','supply-donation-win #litigant_deptid')							
									$($dept_window).attr('callback-deptValue','supply-donation-win #litigant_deptname')
									
									sysmanMain.deptUserSelect()
								}							
							})
							
							break;
							case 'inquiry_item':
							var _mainSelect = $(_options.form).find('#inquiry_item')
							$(_mainSelect).combobox({
								width:el_width,
								required:true,
								readonly:false,
								textField:'name',
								valueField:'name',
								url:_rootpath + _typeUrl +'?id=' + item_code_dic_id,
								onSelect:function(record){
									var _item = $(_options.form).find('#inquiry_item_code')
									$(_item).val(record.id)
									
								},
								onLoadSuccess:function(){
//									if(selectedRecord && selectedRecord.inquiry_item_code){
//										$(_mainSelect).combobox('select',selectedRecord.inquiry_item_code)										
//									}
								}							
							})
							
							break;
							
							case 'affirm_result':
							//认定意见
							var _confirmSelect = $(_options.form).find('#affirm_result')
							$(_confirmSelect).combobox({
								width:el_width,
								required:true,
								readonly:false,
								textField:'name',
								valueField:'name',
								url:_rootpath + _typeUrl +'?id=' + 112,
								onSelect:function(record){
									var _item = $(_options.form).find('#affirm_result_code')
									$(_item).val(record.id)
									
									var $username = $(_options.form).find('#affirm_name')
									var $userid = $(_options.form).find('#affirm_id')
									
									var curUser = sysmanMain.getCurrentUser()
									$($username).textbox('setValue',curUser.name)
									$($userid).val(curUser.id)
								},
								onLoadSuccess:function(){
									if(selectedRecord && selectedRecord.affirm_result_code){
										$(_confirmSelect).combobox('select',selectedRecord.affirm_result_code)									
									}
								}
							})
							
							break;
						}
					}
//					$($form).form('enableValidation').form('validate')
				}
			}
		}
			return public_method;
		});